/*
 * @Author: caiqi
 * @Date: 2019-02-14 14:34:34
 * @Last Modified by: cai.qi
 * @Last Modified time: 2020-05-11 09:59:04
 * @Describe:
 */

"use strict";

import { Service } from "egg";

export default class TypeService extends Service {
    // ------------------------------ 获取所有订单
    async get(_openid: any) {
        const { ctx, app } = this;
        const sequelize = app.Sequelize;
        const data = await ctx.model.ModOrder.findAll({
            where: { status: 1, openid: _openid },
            order: [['created_at', 'DESC']],
            attributes: [
                'id', 'selectedNum',
                // 'introduction', 'introduce', 'price', 'oldprice',
                // 'specifications',
                // 'cover', 'collection',
                // 'created_at', 'folder',
                // [sequelize.col('commodity.id'), 'type_id'],
                'state',
                [sequelize.col('commodity.tag'), 'tag'],
                [sequelize.col('commodity.specifications'), 'specifications'],
                [sequelize.col('commodity.name'), 'name'],
                [sequelize.col('commodity.price'), 'price'],
                [sequelize.col('commodity.introduction'), 'introduction'],
                [sequelize.col('commodity.oldprice'), 'oldprice'],
                [sequelize.col('commodity.cover'), 'cover'],

            ],
            include: [
                {
                    model: app.model.ModCommodity,
                    as: 'commodity',
                    // 指定关联表查询属性，这里表示不需要任何关联表的属性
                    attributes: [],
                    // through: {
                    //     // 指定中间表的属性，这里表示不需要任何中间表的属性
                    //     attributes: [['project.projectName', 'projectName']],
                    // }
                },
            ],

            raw: true,
            // 这个需要和上面的中间表属性配合，表示不忽略include属性中的attributes参数
            includeIgnoreAttributes: true,
        });
        return data;
    }

    async detail(_id: any, _openid: any) {
        const { ctx, app } = this;
        const sequelize = app.Sequelize;
        const data = await ctx.model.ModOrder.findOne({
            where: { status: 1, id: _id, openid: _openid },
            order: [['created_at', 'DESC']],
            attributes: [
                'id', 'selectedNum',
                // 'introduction', 'introduce', 'price', 'oldprice',
                // 'specifications',
                // 'cover', 'collection',
                // 'created_at', 'folder',
                // [sequelize.col('commodity.id'), 'type_id'],
                'state',
                [sequelize.col('commodity.tag'), 'tag'],
                [sequelize.col('commodity.specifications'), 'specifications'],
                [sequelize.col('commodity.name'), 'name'],
                [sequelize.col('commodity.price'), 'price'],
                [sequelize.col('commodity.introduction'), 'introduction'],
                [sequelize.col('commodity.oldprice'), 'oldprice'],
                [sequelize.col('commodity.cover'), 'cover'],

            ],
            include: [
                {
                    model: app.model.ModCommodity,
                    as: 'commodity',
                    // 指定关联表查询属性，这里表示不需要任何关联表的属性
                    attributes: [],
                    // through: {
                    //     // 指定中间表的属性，这里表示不需要任何中间表的属性
                    //     attributes: [['project.projectName', 'projectName']],
                    // }
                },
            ],

            raw: true,
            // 这个需要和上面的中间表属性配合，表示不忽略include属性中的attributes参数
            includeIgnoreAttributes: true,
        });
        return data;
    }

    // ------------------------------ 创建订单
    async create(body: any) {
        const { ctx } = this;
        // const type = await ctx.model.ModOrder.findOne({ where: { name: body.name, status: 1 } });
        // if (type) {
        //     return ctx.throw('该品类已存在', 201);
        // }
        const data = await ctx.model.ModOrder.create(body);
        return data;
    }
    // ------------------------------ 修改订单
    async updata(id: any, body: any) {
        const { ctx } = this;
        const type = await ctx.model.ModOrder.findOne({ where: { id } });
        // const body = JSON.parse(JSON.stringify(type))
        body.state++
        console.log('===============', body)
        if (type) {
            await type.update(body);
            return {};
        } else {
            return ctx.throw('无此ID', 201);
        }
    }
    async del(id: any) {
        const { ctx } = this;
        const type = await ctx.model.ModOrder.findOne({ where: { id } });

        if (type) {
            await type.update({ ...type, status: 0 });
            return {};
        } else {
            return ctx.throw('无此ID', 201);
        }
    }
}
